﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class AnimateCtrl : MonoBehaviour
{
    public float moveSpeed;
    private float moveSpeedLimit = 1.5f;
    public float tippingLen = 100f;
    private Animator anim;
    private Vector3 dir;              //方向
    private Vector3 pos;              //上一次判断时的位置
    public GameObject chearPos;        //椅子的位置        
    public float rage;       //椅子检测范围
    private float count;   //计时器
    public float deltaT;  //判断间隔时间
    private string lastTrigger;  //上一个调用的trigger
    private int rangeNum;   //随机整数
    public GameObject Bubble;
    public Vector3 bufferPos;
    private GameObject bubble;
    public bool isSitting = false;


    // Start is called before the first frame update
    void Start()
    {
        moveSpeed = moveSpeedLimit;
        bufferPos = transform.position;

        bubble = GameObject.Instantiate(Bubble, new Vector3(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y + 5f, GetComponent<Transform>().position.z), GetComponent<Transform>().rotation);
        bubble.transform.localScale = bubble.transform.localScale * 4;
        bubble.GetComponent<BubbleFollow>().Target = gameObject.transform;
        bubble.GetComponent<BubbleSwitch>()._current = BubbleSwitch.BubbleStatus.Darkgray;
        anim = gameObject.GetComponent<Animator>();
        count = 0;
        chearPos = GameObject.FindWithTag("WoodBench");
    }
    // Update is called once per frame
    void Update()
    {

    }
    private void OnDestroy()
    {
        GameObject.FindWithTag("WoodBench").GetComponent<CheckSitNum>().Players.Remove(gameObject);

        Destroy(bubble);
    }
    private void FixedUpdate()
    {
        Moving();
    }
    void Moving()
    {
        if (count == 0)
        {
            pos = transform.position;
        }
        count += Time.deltaTime;
        if (count >= deltaT)
        {
            if ((transform.position - pos).magnitude >= tippingLen)
            {
                moveSpeed = moveSpeedLimit;
                anim.speed = 1;
                if (!string.IsNullOrEmpty(lastTrigger))
                    anim.ResetTrigger(lastTrigger);
                anim.SetTrigger("walk");
                anim.speed = moveSpeed;
                lastTrigger = "walk";
                dir = transform.position - pos;
                transform.rotation = Quaternion.LookRotation(dir);    //转向移动方向
            }
            else if ((transform.position - chearPos.transform.position).magnitude <= 5 * rage)
            {
                if (isSitting)
                {
                    dir = new Vector3(0, 0, -1);
                    transform.rotation = Quaternion.LookRotation(dir);    //强制转向正面
                    if (!string.IsNullOrEmpty(lastTrigger))
                        anim.ResetTrigger(lastTrigger);
                    rangeNum = Random.Range(0, 4);
                    if (rangeNum % 2 == 0)
                    {
                        anim.SetTrigger("sit1");
                        lastTrigger = "sit1";

                    }
                    else
                    {
                        anim.SetTrigger("sit2");
                        lastTrigger = "sit2";
                    }
                }
                else
                {
                    moveSpeed = moveSpeedLimit;
                    anim.speed = 1;
                    if (!string.IsNullOrEmpty(lastTrigger))
                        anim.ResetTrigger(lastTrigger);
                    anim.SetTrigger("stop");
                    lastTrigger = "stop";
                }

            }
            else
            {
                moveSpeed = moveSpeedLimit;
                anim.speed = 1;
                if (!string.IsNullOrEmpty(lastTrigger))
                    anim.ResetTrigger(lastTrigger);
                anim.SetTrigger("stop");
                lastTrigger = "stop";
            }
            count = 0;
        }
    }
    void BufferPos()
    {
        bufferPos = transform.position;
        bufferPos.y += 1f;
    }
    private void OnTriggerEnter(Collider collision)
    {
        if (collision.gameObject.tag == "WoodBench")
        {
            Debug.Log("Collision Sitting");
            isSitting = true;
        }
    }
    private void OnTriggerExit(Collider collision)
    {
        if (collision.gameObject.tag == "WoodBench")
        {
            isSitting = false;
        }
    }
}
